<template>
	<view>
		<view class="uni-list">
			<view v-for="(value, index) in listData" :key="index">
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" @click="goDetail(value)">
					<view class="uni-media-list">
						<image class="uni-media-list-logo"
							:src="value.content.titleImg ? value.content.titleImg : moren" mode="widthFix"></image>
						<view class="uni-media-list-body">
							<view class="uni-media-list-text-top">{{ value.content.title }}</view>
							<view class="uni-media-list-text-bottom">
								<text>{{ value.content.author }}</text>
								<text>{{ formatDateTime(value.contributeDate) }}</text>
							</view>
							<view class="bottom">
								<u-button style="width: 50px;" type="error" @click="pupop(value)">删除</u-button>
								<u-modal width="233px" v-model="delModal.show" :content="delModal.content"
								 :show-cancel-button="true" @confirm="btn_delete()"></u-modal>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import request from '../../util/request.js';
	import api from '../../util/api.js';
	import {
		dateUtils
	} from '../../util/util.js';
	export default {
		data() {
			return {
				listData: [],
				moren: '/static/logo.png',
				maxPage: 0,
				NumberPages: '',
				tem: '',
				pageNo: 1,
				pageSize: 10,
				username: '',
				format: 'json',
				Boolean: true,
				url: '/pages/draft/draft',
				selectedItem: '',
				show: true,
				delModal: {
					show: false,
					content: "确定要删除？"
				},
			}
		},
		onShow() {
			
		},
		onLoad() {
			this.username = uni.getStorageSync('username');
			this.jiazaizhong();
			this.getList();
		},
		onPullDownRefresh() {
			this.getList();
		},
		onReachBottom() {
			if (this.pageNo > this.NumberPages) {
				uni.showToast({
					title: '已经到底啦',
					icon: 'none'
				});
				return false;
			}
			this.jiazaizhong();
			this.pageNo = this.pageNo + 1;
			this.getConcatList();
		},
		methods: {
			pupop(value) {
				this.selectedItem = value;
				console.log('aaaa=', this.selectedItem);
				this.delModal.show = true;
			},
			btn_delete() {
				let e = this.selectedItem;
				let url = api.delete;
				url = url + "?id=" + e.content.infoText.infoId + "&session=&username=" + this.username
				request.getd(url, {

				}).then(response => {
					
					if(response.data.code === 0){
						uni.showToast({
							title: '删除成功。',
							icon: 'success'
						});
						this.DELETEjia()
					} else {
						uni.showToast({
							icon:'none',
							title:'删除失败，' + response.data.msg,
						})
					}
				}).catch(ex => {
					console.log('ex=', ex);
					uni.showToast({
						icon: 'none',
						title: '请求失败，请稍后再试'
					})
				})
			},
			goDetail: function(e) {
				let detail = {
					title: e.content.title,
					author: e.content.author,
					txt: e.content.infoText.txt,
					username: this.username
				};
				wx.reLaunch({
					url: '/pages/tougao/tougao?content=' + encodeURIComponent(JSON
						.stringify(detail))
				})
			},
			jiazaizhong() {
				if (this.Boolean) {
					uni.showLoading({
						title: '加载中'
					});
				}
			},
			DELETEjia() {
				if (this.Boolean) {
					uni.showLoading({
						title: '删除中'
					});
					// this.pageNo = 1;
					this.getList();
				}
			},
			getList() {
				let url = api.listDraft;
				url = url + "?format=" + this.format + "&pageNo=" + this.pageNo + "&pageSize=" + this.pageSize +
					"&session=&username=" + this.username;
				request.getf(url, {
					username: this.username
				}).then(response => {
					if (response.data.code === 0) {
						this.listData = response.data.data.list;
						this.maxPage = response.data.data.count;
						this.tem = this.maxPage / 10;
						this.NumberPages = this.tem > 1 ? this.tem : 1;
						uni.stopPullDownRefresh();
						uni.hideLoading();
					} else {
						if (ret.data.data.list.length < 1) {
							uni.showToast({
								title: '没有数据了',
								icon: 'none'
							});
							return false;
						}
						uni.showToast({
							icon: 'none',
							title: '请求失败，' + response.data.msg,
						})
					}
				}).catch(ex => {
					uni.showToast({
						icon: 'none',
						title: '请求失败，请稍后再试'
					})
				})
			},
			getConcatList() {
				let url = api.listDraft;
				url = url + "?format=" + this.format + "&pageNo=" + this.pageNo + "&pageSize=" + this.pageSize +
					"&session=&username=" + this.username;
				request.getf(url, {
					username: this.username
				}).then(response => {
					if (response.data.code === 0) {
						this.listData = this.listData.concat(response.data.data.list);
						uni.hideLoading();
					} else {
						uni.showToast({
							icon: 'none',
							title: '请求失败，' + response.data.msg,
						})
					}
				}).catch(ex => {
					uni.showToast({
						icon: 'none',
						title: '请求失败，请稍后再试'
					})
				})
			},
			addZero(num) {
				return num < 10 ? '0' + num : num;
			},
			formatDateTime(date) {
				const time = new Date(Date.parse(date));
				time.setTime(time.setHours(time.getHours()));
				const Y = time.getFullYear() + '.';
				const M = this.addZero(time.getMonth() + 1) + '.';
				const D = this.addZero(time.getDate()) + ' ';
				const h = this.addZero(time.getHours()) + ':';
				const m = this.addZero(time.getMinutes()) + ':';
				const s = this.addZero(time.getSeconds());
				return Y + M + D + h + m + s;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.uni-media-list-logo {
		width: 180rpx;
		height: 140rpx;
	}

	.uni-media-list-body {
		height: auto;
		justify-content: space-around;
	}

	.uni-media-list-text-top {
		height: 74rpx;
		font-size: 28rpx;
		overflow: hidden;
	}

	.uni-media-list-text-bottom {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	.uni-media-list {
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		width: 750px;
		flex-direction: row;
	}

	.loadMore {
		margin-top: 0px;
		width: 100%;
		line-height: 10em;
		font-size: 30px;
		color: #555;

	}


	.uni-list-cell {
		position: relative;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.uni-list {
		background-color: #FFFFFF;
		position: absolute;
		width: 100%;
		display: flex;
		margin-bottom: 100px;
	}

	.bottom {
		display: flex;
		padding: 0 390rpx;
		padding-right: 50px;
		margin-top: 25px;
		margin-bottom: 10px;
		justify-content: right;
		align-items: center;

		.btn {
			margin-bottom: -10px;
			margin-left: 50px;
			line-height: 45rpx;
			width: 80rpx;
			border-radius: 26rpx;
			border: 2rpx solid #00BFFF;
			font-size: 26rpx;
			text-align: center;
			color: #00BFFF;
		}

		.evaluate {
			color: #DD524D;
			border-color: #DD524D;
		}
	}
</style>
